﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Documents.Serialization;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
using System.Printing;
using System.Windows;
using System.Xml;

namespace Lds.Ward.PhotoDirectory.WpfApp {
	class XamlSerializerWriter : SerializerWriter {
		public XamlSerializerWriter(Stream stream) {
			this.stream = stream;
		}

		Stream stream;

		public override void CancelAsync() {
			throw new NotImplementedException();
		}

		public override SerializerWriterCollator CreateVisualsCollator(PrintTicket documentSequencePT, PrintTicket documentPT) {
			throw new NotImplementedException();
		}

		public override SerializerWriterCollator CreateVisualsCollator() {
			throw new NotImplementedException();
		}

		/// <summary>
		/// Write a single FixedDocumentSequence and close package
		/// </summary>
		public override void Write(FixedDocumentSequence fixedDocumentSequence, PrintTicket printTicket) {
			serializeObjectTree(fixedDocumentSequence);
		}

		/// <summary>
		/// Write a single FixedDocumentSequence and close package
		/// </summary>
		public override void Write(FixedDocumentSequence fixedDocumentSequence) {
			Write(fixedDocumentSequence, null);
		}

		/// <summary>
		/// Write a single FixedDocument and close package
		/// </summary>
		public override void Write(FixedDocument fixedDocument, PrintTicket printTicket) {
			serializeObjectTree(fixedDocument);
		}

		/// <summary>
		/// Write a single FixedDocument and close package
		/// </summary>
		public override void Write(FixedDocument fixedDocument) {
			Write(fixedDocument, null);
		}


		/// <summary>
		/// Write a single FixedPage and close package
		/// </summary>
		public override void Write(FixedPage fixedPage, PrintTicket printTicket) {
			serializeObjectTree(fixedPage);
		}

		/// <summary>
		/// Write a single FixedPage and close package
		/// </summary>
		public override void Write(FixedPage fixedPage) {
			Write(fixedPage, null);
		}

		/// <summary>
		/// Write a single DocumentPaginator and close package
		/// </summary>
		public override void Write(DocumentPaginator documentPaginator, PrintTicket printTicket) {
			serializeObjectTree(documentPaginator.Source);
		}

		/// <summary>
		/// Write a single DocumentPaginator and close package
		/// </summary>
		public override void Write(DocumentPaginator documentPaginator) {
			Write(documentPaginator, null);
		}

		/// <summary>
		/// Write a single Visual and close package
		/// </summary>
		public override void Write(Visual visual, PrintTicket printTicket) {
			serializeObjectTree(visual);
		}

		/// <summary>
		/// Write a single Visual and close package
		/// </summary>
		public override void Write(Visual visual) {
			Write(visual, null);
		}

		public override void WriteAsync(FixedDocumentSequence fixedDocumentSequence, PrintTicket printTicket, object userState) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedDocumentSequence fixedDocumentSequence, object userState) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedDocumentSequence fixedDocumentSequence, PrintTicket printTicket) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedDocumentSequence fixedDocumentSequence) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedDocument fixedDocument, PrintTicket printTicket, object userState) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedDocument fixedDocument, object userState) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedDocument fixedDocument, PrintTicket printTicket) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedDocument fixedDocument) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedPage fixedPage, PrintTicket printTicket, object userState) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedPage fixedPage, object userState) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedPage fixedPage, PrintTicket printTicket) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(FixedPage fixedPage) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(DocumentPaginator documentPaginator, PrintTicket printTicket, object userState) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(DocumentPaginator documentPaginator, object userState) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(DocumentPaginator documentPaginator, PrintTicket printTicket) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(DocumentPaginator documentPaginator) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(Visual visual, PrintTicket printTicket, object userState) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(Visual visual, PrintTicket printTicket) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(Visual visual, object userState) {
			throw new NotImplementedException();
		}

		public override void WriteAsync(Visual visual) {
			throw new NotImplementedException();
		}

		public override event WritingCancelledEventHandler WritingCancelled;

		public override event WritingCompletedEventHandler WritingCompleted;

		public override event WritingPrintTicketRequiredEventHandler WritingPrintTicketRequired;

		public override event WritingProgressChangedEventHandler WritingProgressChanged;

		void serializeObjectTree(object objectTree) {
			using (TextWriter writer = new StreamWriter(stream)) {
				using (XmlWriter xmlWriter = new XmlTextWriter(writer)) {
					// Set serialization mode
					XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(xmlWriter);
					// Serialize
					XamlWriter.Save(objectTree, manager);
				}
			}
		}
	}
}
